package com.qf.servlet;

import com.qf.entity.Admin;
import com.qf.util.DateUtils;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.IOUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.lang.reflect.Field;
import java.util.List;
import java.util.UUID;

//ada
@WebServlet(urlPatterns = "/registerfile")
public class RegisterFileServlet extends HttpServlet {
    public  static  String uploadedPath="D:\\Program Files\\upload\\";
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        DiskFileItemFactory fileItemFactory =new DiskFileItemFactory();
        ServletFileUpload servletFileUpload = new ServletFileUpload(fileItemFactory);

        servletFileUpload.setHeaderEncoding("utf-8");

        List<FileItem> items =null;
        try {
            items =servletFileUpload.parseRequest(req);
        }catch (FileUploadException e){
            e.printStackTrace();
        }

        Admin admin = new Admin();
        Class<? extends Admin> cls = admin.getClass();
        if (items !=null){
            for (FileItem item :items){
                if (item.isFormField()){
                //普通项
                System.out.println("表单的名称："+item.getFieldName()+"值："+item.getString("utf-8"));

//                if ("username".equals(item.getFieldName())){
//                    admin.setUsername(item.getString("utf-8"));
//                    }
                if ("birthday".equals(item.getFieldName())){
                    //特殊处理birthday属性
                    admin.setBrithday(item.getString("utf-8"));
                }else if ("power".equals(item.getFieldName())){
                    //特殊处理权限类型
                    admin.addPower(item.getString("utf-8"));
                }else {
                    try {
                        //getDeclaredField获得私有的成员变量
                        Field field =cls.getDeclaredField(item.getFieldName());
                        field.setAccessible(true);//授权，可以操作私有的成员变量
                        field.set(admin,item.getString("utf-8"));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }


                  //一堆key-value -> Admin对象的对应属性在 ->
                    //获得Admin这个类的反射对象(Class对象)


                }else {
                    //一个上传项
                    System.out.println("上传的文件："+item.getName());
                    String filename = UUID.randomUUID().toString();

                    try (
                        //输入流
                        InputStream in = item.getInputStream();
                        OutputStream out = new FileOutputStream(uploadedPath + filename);

                        ){
                        IOUtils.copy(in,out);
                        admin.setHeader(filename);
                    }


                }

            }

        }
        DateUtils.addAdmin(admin);
        resp.sendRedirect("login.jsp");
        System.out.println("注册用户信息："+admin);
    }

}
